package com.zeng.sys.filemd.servicemodule.util;

import com.zeng.sys.authmd.apimodule.vo.UserVo;
import com.zeng.sys.filemd.servicemodule.service.FileRecordService;
import com.zeng.sys.filemd.servicemodule.service.FileServerService;
import com.zeng.sys.filemd.servicemodule.service.FileTypeService;
import com.zeng.sys.filemd.servicemodule.vo.FileRecordVo;
import com.zeng.sys.filemd.servicemodule.vo.FileServerVo;
import com.zeng.sys.filemd.servicemodule.vo.FileTypeVo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.io.IOException;
import java.util.Date;
import java.util.List;

public class MultipartFileUtil {

    @Autowired
    FileTypeService fileTypeService;

    @Autowired
    FileServerService fileServerService;

    @Autowired
    FileRecordService fileRecordService;


    public FileRecordVo handleFileRecordVo(MultipartRequest multipartRequest, FileRecordVo fileRecordVo, UserVo userVo) {
        MultipartFile multipartFile = multipartRequest.getFileNames().hasNext() ? multipartRequest.getFile(multipartRequest.getFileNames().next()) : null;
        if (multipartFile == null) {
            return null;
        }
        FileServerVo fileServerVo = null;

        if (fileServerVo == null) {
            List<FileServerVo> all = fileServerService.findAll(null);
            if (all != null && all.size() > 0) {
                fileServerVo = all.get(0);
            }
            if (fileServerVo == null) {
                throw new RuntimeException("没有可以用的服务器");
            }
        }
        fileRecordVo.setFileServerVo(fileServerVo);
        FileTypeVo typeVo = fileTypeService.getOneByCode(fileRecordVo.getFileTypeCode());

        fileRecordVo.setName(multipartFile.getOriginalFilename());
        fileRecordVo.setSize(String.valueOf(multipartFile.getSize()));
        fileRecordVo.setNickName(userVo.getFullName());
        fileRecordVo.setStatus("正常");
        fileRecordVo.setType(multipartFile.getContentType());
        fileRecordVo.setFileServerCode(fileServerVo.getCode());
        fileRecordVo.setPath(typeVo.getFlag() + "/" + DateFormatUtils.format(new Date(), "yyyy-MM") + "/" + multipartFile.getOriginalFilename());
        try {
            fileRecordVo.setDataWithByte(multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileRecordVo;
    }

    public FileRecordVo handleFileRecordVo(MultipartRequest multipartRequest, UserVo userVo, String typeName) {
        MultipartFile multipartFile = multipartRequest.getFileNames().hasNext() ? multipartRequest.getFile(multipartRequest.getFileNames().next()) : null;
        if (multipartFile == null) {
            return null;
        }
        FileServerVo fileServerVo = null;
        FileRecordVo fileRecordVo = new FileRecordVo();

        if (fileServerVo == null) {
            List<FileServerVo> all = fileServerService.findAll(null);
            if (all != null && all.size() > 0) {
                fileServerVo = all.get(0);
            }
            if (fileServerVo == null) {
                throw new RuntimeException("没有可以用的服务器");
            }
        }
        fileRecordVo.setFileServerVo(fileServerVo);
        List<FileTypeVo> all = fileTypeService.findAll(FileTypeVo.builder().name(typeName).build());
        if (all.size() <= 0) {
            throw new RuntimeException("没有查询到可以用类型");
        }
        FileTypeVo typeVo = all.get(0);

        fileRecordVo.setFileTypeCode(typeVo.getCode());
        fileRecordVo.setName(multipartFile.getOriginalFilename());
        fileRecordVo.setSize(String.valueOf(multipartFile.getSize()));
        fileRecordVo.setNickName(userVo.getFullName());
        fileRecordVo.setStatus("正常");
        fileRecordVo.setType(multipartFile.getContentType());
        fileRecordVo.setFileServerCode(fileServerVo.getCode());
        fileRecordVo.setPath(typeVo.getFlag() + "/" + DateFormatUtils.format(new Date(), "yyyy-MM") + "/" + multipartFile.getOriginalFilename());
        try {
            fileRecordVo.setDataWithByte(multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRecordVo fileRecordVo1 = fileRecordService.addOne(fileRecordVo);
        fileRecordVo.setCode(fileRecordVo1.getCode());
        return fileRecordVo;
    }

    public FileRecordVo handleFileRecordVo(byte[] bytes, UserVo userVo, String typeName,String fileName,String contentType) {
        if (bytes == null) {
            return null;
        }
        FileServerVo fileServerVo = null;
        FileRecordVo fileRecordVo = new FileRecordVo();

        if (fileServerVo == null) {
            List<FileServerVo> all = fileServerService.findAll(null);
            if (all != null && all.size() > 0) {
                fileServerVo = all.get(0);
            }
            if (fileServerVo == null) {
                throw new RuntimeException("没有可以用的服务器");
            }
        }
        fileRecordVo.setFileServerVo(fileServerVo);
        List<FileTypeVo> all = fileTypeService.findAll(FileTypeVo.builder().name(typeName).build());
        if (all.size() <= 0) {
            throw new RuntimeException("没有查询到可以用类型");
        }
        FileTypeVo typeVo = all.get(0);

        fileRecordVo.setFileTypeCode(typeVo.getCode());
        fileRecordVo.setName(fileName);
        fileRecordVo.setSize(String.valueOf(bytes.length));
        fileRecordVo.setNickName(userVo.getFullName());
        fileRecordVo.setStatus("正常");
        fileRecordVo.setType(contentType);
        fileRecordVo.setFileServerCode(fileServerVo.getCode());
        fileRecordVo.setPath(typeVo.getFlag() + "/" + DateFormatUtils.format(new Date(), "yyyy-MM") + "/" + fileName);
        fileRecordVo.setDataWithByte(bytes);

        FileRecordVo fileRecordVo1 = fileRecordService.addOne(fileRecordVo);
        fileRecordVo.setCode(fileRecordVo1.getCode());
        return fileRecordVo;
    }

    public FileRecordVo handleFileRecordVoOnServer(FileRecordVo fileRecordVo) {
        FileServerVo fileServerVo = null;

        if (fileServerVo == null) {
            fileServerVo = fileServerService.getOneByCode(fileRecordVo.getFileServerCode());
            if (fileRecordVo == null) {
                throw new RuntimeException("没有可以用的服务器");
            }
        }
        fileRecordVo.setFileServerVo(fileServerVo);
        return fileRecordVo;
    }

}
